package com.xinfu.mooddiaryjava.ui.page;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kunminx.architecture.ui.adapter.SimpleBaseBindingAdapter;
import com.xinfu.mooddiaryjava.R;
import com.xinfu.mooddiaryjava.bridge.state.MainFragmentViewModel;
import com.xinfu.mooddiaryjava.data.bean.StateBean;
import com.xinfu.mooddiaryjava.databinding.FragmentMainBinding;
import com.xinfu.mooddiaryjava.databinding.RecyclerFragmentMainBinding;
import com.xinfu.mooddiaryjava.ui.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

public class MainFragment extends BaseFragment {

    private MainFragmentViewModel mViewModel;
    private FragmentMainBinding mBinding;
    private SimpleBaseBindingAdapter<StateBean, RecyclerFragmentMainBinding> mAdapter;
    private Fragment fragment;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewModel = getFragmentViewModelProvider(this).get(MainFragmentViewModel.class);
        fragment = this;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        mBinding = FragmentMainBinding.bind(view);
        mBinding.setVm(mViewModel);
        mBinding.setClick(new ClickProxy());
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mAdapter = new SimpleBaseBindingAdapter<StateBean, RecyclerFragmentMainBinding>(getContext(),R.layout.recycler_fragment_main) {
            @Override
            protected void onSimpleBindItem(RecyclerFragmentMainBinding binding, StateBean item, RecyclerView.ViewHolder holder) {
                binding.fragmentMainRecyclerTv.setText(item.getText());
                binding.fragmentMainRecyclerTv.setBackgroundColor(holder.getLayoutPosition()%2 == 0 ?getActivity().getResources().getColor(R.color.colorYellow) : getActivity().getResources().getColor(R.color.colorBlue));
                binding.getRoot().setOnClickListener(v ->  {
                    Bundle bundle = new Bundle();
                    bundle.putString("state", item.getText());
                    NavHostFragment
                            .findNavController(fragment)
                            .navigate(R.id.action_mainFragment_to_contentListFragment, bundle);
                });
            }
        };
        mBinding.fragmentMainRv.setAdapter(mAdapter);
        List<StateBean> list = new ArrayList<>();
        list.add(new StateBean("开心"));
        list.add(new StateBean("愤怒"));
        list.add(new StateBean("后悔"));
        list.add(new StateBean("哀伤"));
        list.add(new StateBean("励志"));
        list.add(new StateBean("日记"));

        mAdapter.setList(list);
        mAdapter.notifyDataSetChanged();
        // TODO: Use the ViewModel
    }

    public class ClickProxy{

    }
}
